//
//  WSNavBarTitleView.m
//  WeiboSpace
//
//  Created by nan4 li on 12-5-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "WSNavBarTitleView.h"

@interface WSNavBarTitleView()
-(void)removeDomalView;
@end
@implementation WSNavBarTitleView
@synthesize domalView;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBackgroundColor:[UIColor clearColor]];
        
        //开始按钮
        UIButton *startButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
        [startButton setBackgroundColor:[UIColor blueColor]]; 
        [self addSubview:startButton];
        [startButton release];
        
        //初始化
        UIButton *textButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, frame.size.width-30,40)];
        [textButton setTitle:@"张学友" forState:UIControlStateNormal];
        [textButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [textButton setBackgroundColor:[UIColor yellowColor]]; 
        [textButton addTarget:self action:@selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:textButton];
        [textButton release];
        
    }
    return self;
}

-(void)dealloc
{
    self.domalView  = nil;
    [super dealloc];
}
-(void)textButtonClicked:(id)sender
{

    if(domalView==nil||domalView.frame.size.height==0)
    {
    UIView *testView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, self.frame.size.width, 0)];
    [testView setBackgroundColor:[UIColor darkGrayColor]];
    self.domalView = testView;
    [testView setAlpha:0.0f];
    [self addSubview:testView];
    [testView release];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.5f];
    testView.frame = CGRectMake(0, 40, self.frame.size.width, 100);
    [testView setAlpha:1.0f];
    [UIView commitAnimations];
    }
    else 
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDuration:0.5f];
        domalView.frame = CGRectMake(0, 40, self.frame.size.width, 0);
        [domalView setAlpha:0.0f];
        [UIView commitAnimations];
        //[domalView removeFromSuperview];
        //[self performSelector:@selector(removeDomalView)];
        [self performSelector:@selector(removeDomalView) withObject:nil afterDelay:0.2f];
    }
}

-(void)removeDomalView
{
    //这里点击过快会有内存泄露
    [domalView removeFromSuperview];
}

#pragma overwright UIResponder
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    
    CGPoint pt = [touch locationInView:self];
    if (!CGRectContainsPoint(domalView.frame, pt)) 
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDuration:0.5f];
        domalView.frame = CGRectMake(0, 40, self.frame.size.width, 0);
        [domalView setAlpha:0.0f];
        [UIView commitAnimations];
        
    }
}

@end
